#!/bin/bash
 
#mysql安装包名
MYSQL_VERSION='mysql-5.6.39-linux-glibc2.12-x86_64'
 
 
#mysql安装的位置
MYSQL_INSTALL_PATH='/usr/local'
 
#存放二进制包的路径
SOFTWARE_PATH='/opt/software'
 
#1 步骤输出 紫色
#2 正确输出 绿色
#3 错误输出 红色
#4 提示输出 蓝色
#5 警告输出 黄色
function echo_fun(){
  if [ $# -ge 2 ];then
     params_num=$1
     shift 1
     params_mes=$@
  else
    echo_fun 3 请至少输入两个参数 echo_fun ..
    exit
  fi
  case $params_num in
        1)
        echo -e "\033[35;40;1m  ***************************** ${params_mes} *****************************\033[0m\r\n"
        ;;
        2)
        echo -e "\033[32;40;1m ${params_mes}\033[0m\r\n"
        ;;
        3)
        echo -e "\033[31;40;1m ${params_mes}\033[0m\r\n"
        ;;
        4)
        echo -e "\033[36;40;1m ${params_mes}\033[0m\r\n"
        ;;
        5)
        echo -e "\033[33;40;1m ${params_mes} \033[0m\r\n"
        ;;
        *)
        echo_fun 3 参数异常第一个参数应为1,2,3,4,5
        ;;
   esac
}
 
echo_fun 1 mysql的HA部署安装
 
#检查是否安装mysql
function check_catalog_exist(){
 echo_fun 4 检查$1机器上是否安装了mysql
 num=$(ssh $1 test -d $2 &&  echo 1 || echo 0 )
 if [ $num -eq 1 ];then
   echo_fun 3 "$1机器上的$2目录存在,请注意检查"
   exit
 fi
}
 
#检查是否存在mysql用户
function  check_user_mysql(){
  echo_fun 4 检查$1机器是否存在mysql用户
  num=$(ssh $1 "cat /etc/passwd|grep -w mysql|wc -l" )
  if [ $num -eq 1 ];then
     echo_fun 3 "$ip机器上的mysql用户已经存在,请注意检查"
     exit
  else
     ssh $1 "useradd mysql"
  fi
 
}
 
 
#本地解压,发送到主从机器
function tar_mysql(){
	
cd ${SOFTWARE_PATH}
tar -xf ${MYSQL_VERSION}.tar.gz  -C  ${MYSQL_INSTALL_PATH}
ln -s ${MYSQL_INSTALL_PATH}/${MYSQL_VERSION}   ${MYSQL_INSTALL_PATH}/mysql
chown -R mysql:mysql ${MYSQL_INSTALL_PATH}/mysql*
cd ${MYSQL_INSTALL_PATH}
chown -R mysql:mysql mysql*
 
rsync -av mysql  ${MYSQL_VERSION}  $1:${MYSQL_INSTALL_PATH} >/dev/null 2>&1
 
echo_fun 2 主机器mysql安装目录已经同步完毕
 
rsync -av mysql  ${MYSQL_VERSION}  $2:${MYSQL_INSTALL_PATH} >/dev/null 2>&1
 
echo_fun 2 从机器mysql安装目录已经同步完毕
 
echo_fun 4 删除本地的解压包
rm -rf ${MYSQL_INSTALL_PATH}/mysql*
}
 
 
 
 
#修改主机器上/etc/my.cnf
function  alter_master_file(){
ssh -Tq $1 <<remotessh
	rm -rf /etc/my.cnf
	cd  ${MYSQL_INSTALL_PATH}/mysql
	cp support-files/my-default.cnf  /etc/my.cnf
	cp support-files/mysql.server  /etc/init.d/mysql.server
	sed -i '/mysqld/a\log-bin-index=master-bin.index' /etc/my.cnf
	sed -i '/mysqld/a\log-bin=master-bin' /etc/my.cnf
	sed -i '/mysqld/a\server-id=1' /etc/my.cnf
	exit
remotessh
}
 
#修改从机器上/etc/my.cnf
function  alter_slave_file(){
ssh -Tq $1 <<remotessh
	rm -rf /etc/my.cnf
	cd ${MYSQL_INSTALL_PATH}/mysql
	cp support-files/my-default.cnf  /etc/my.cnf
	cp support-files/mysql.server  /etc/init.d/mysql.server
	sed -i '/mysqld/a\relay-log=slave-relay-bin' /etc/my.cnf
	sed -i '/mysqld/a\relay-log-index=slave-relay-bin.index' /etc/my.cnf
	sed -i '/mysqld/a\server-id=2' /etc/my.cnf
	exit
remotessh
}
 
#初始化数据库
function init_mysql(){
ssh -Tq $1 <<remotessh
    cd ${MYSQL_INSTALL_PATH}/mysql
	scripts/mysql_install_db --user=mysql  >/dev/null 2>&1
	/etc/init.d/mysql.server start
	exit
remotessh
}
 
 
 
 
echo_fun 2 请输入master机器ip地址
read -p "master_ip=" master_ip
echo ''
#检查主机器mysql安装目录是否存在
check_catalog_exist ${master_ip} ${MYSQL_INSTALL_PATH}/mysql
#检查主机器mysql的启动用户mysql是否存在
#check_user_mysql ${master_ip}
 
 
echo_fun 2 请输入slave机器ip地址
read -p "slave_ip=" slave_ip
echo ''
#检查从机器mysql安装目录是否存在
check_catalog_exist ${slave_ip} ${MYSQL_INSTALL_PATH}/mysql
#检查主机器mysql的启动用户mysql是否存在
#check_user_mysql ${slave_ip}
 
 
#开始安装
echo_fun 4 本机解压，然后发送到主从机器上，时间较长请耐心等待
 
tar_mysql $master_ip $slave_ip
 
echo_fun 4 修改主mysql的配置文件
alter_master_file $master_ip
 
echo_fun 2 主机器配置文件修改完毕
 
echo_fun 4 修改主mysql的配置文件
alter_slave_file $slave_ip
echo_fun 2 从机器配置文件修改完毕
 
 
#初始化mysql,并启动
echo_fun 4 初始化主mysql，并启动主mysql
init_mysql $master_ip
echo ''
 
echo_fun 4 初始化从mysql，并启动从mysql
init_mysql $slave_ip
echo ''
 
#主mysql设置密码和配置
echo_fun 4  初始化主mysql密码，创建同步master端数据库的用户
 
echo_fun 2 请输入root用户密码
read -p "root_passwd=" root_passwd
echo ''
 
echo_fun 2 输入同步master数据库到slave端用户
read -p "replication_user=" r_user
echo ''
 
echo_fun 2 输入用户密码
read -p "replication_passwd=" r_passwd
 
ssh -Tq ${master_ip}<<EOF
mysql  -uroot <<eof 
set password = password("${root_passwd}");
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${root_passwd}';
GRANT ALL PRIVILEGES ON *.* TO 'root'@localhost IDENTIFIED BY '${root_passwd}';
GRANT REPLICATION SLAVE ON *.* to '${r_user}'@'%' identified by '${r_passwd}';
flush privileges;
eof
EOF
 
#postion地址，注意这里不能用$ip ""格式 也不能用awk指令，不知道为什么
 
postion_num=$(ssh -Tq $master_ip <<eof
    mysql -uroot -p${root_passwd} -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
eof
)
 
#bin-log文件名 
log_name=$(ssh -Tq $master_ip <<eof
    mysql -uroot -p${root_passwd} -e 'show master status\G' 2>/dev/null|grep File |cut -f2 -d :
eof
)
 
 
#从mysql设置密码和配置
ssh -Tq ${slave_ip} <<EOF 
mysql  -uroot <<eof
set password = password("${root_passwd}");
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${root_passwd}';
GRANT ALL PRIVILEGES ON *.* TO 'root'@localhost IDENTIFIED BY '${root_passwd}';
change master to master_host='${master_ip}',master_port=3306,master_user='${r_user}',master_password='${r_passwd}', master_log_file='${log_name}',master_log_pos=${postion_num};
start slave;
eof
EOF
 
 
#检查是否主从复制搭建成功
 
running_num=$(ssh -Tq $slave_ip <<eof
   mysql -uroot -p${root_passwd} -e "show slave status\G" 2>/dev/null |grep "Running:"|wc -l
eof
)
 
if [ $running_num -eq 2 ];then
     echo_fun 2 "mysql主从复制搭建成功"     
  else
     echo_fun 3  "mysql主从搭建失败"
fi
